package ding.blog.service;


import com.example.myblog.entity.User;
import com.example.myblog.dao.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService {

    private final UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    // 保存用户
    public User saveUser(User user) {
        return userRepository.save(user);
    }

    // 根据ID获取用户
    public User getUserById(Long id) {
        return userRepository.findById(id).orElse(null);
    }

    // 根据用户名获取用户
    public User getUserByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    // 获取所有用户
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    // 通过用户获取评论
    public java.util.List<com.example.myblog.entity.Comment> getCommentsByUser(User user) {
        return userRepository.findByUser(user);
    }
}